﻿using System;
using System.Drawing;
using System.Diagnostics;

using iPhoneUI.Shell;

namespace iPhoneUI.Widgets
{
    public class PageNavigator : Widget
    {
        private IImage m_activePage;
        private IImage m_inactivePage;

        public PageNavigator()
        {
            ActivePageImage = "bullet_ball_glass_green.png";
            InactivePageImage = "bullet_ball_yellow.png";
        }

        public string ActivePageImage { get; set; }

        public string InactivePageImage { get; set; }

        protected override void OnSetOwner()
        {
            m_activePage = ShellConfig.CreatePerPixelAlphaImage(ActivePageImage);
            m_inactivePage = ShellConfig.CreatePerPixelAlphaImage(InactivePageImage);

            SetInitialSize(new Size(Size.Width, m_activePage.Size().Height));

            base.OnSetOwner();
        }

        public int CurrentIndex { get; set; }

        public int Count { get; set; }

        protected override void OnDraw(Canvas canvas, Rectangle clipBounds)
        {
            // draw the page index indicators
            if (Count > 1)
            {
                Size size = m_activePage.Size();
                const int spacing = 20;
                int start = (Size.Width / 2) - (int)(spacing * ((double)Count / 2.0));
                for (int i = 0; i < Count; i++)
                {
                    Rectangle bounds = new Rectangle(start, Location.Y, size.Width, size.Height);
                    if (i == CurrentIndex)
                        canvas.AlphaBlend(m_activePage, bounds);
                    else
                        canvas.AlphaBlend(m_inactivePage, bounds);

                    start += spacing;
                }
            }
        }

        protected override void OnDispose()
        {
            m_activePage = null;
            m_inactivePage = null;

            base.OnDispose();
        }
    }
}
